package com.sls.web.manager.action.recharge;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.sys.pojo.Permission;
import com.whalin.MemCached.MemCachedClient;

@SuppressWarnings("unchecked")
public class RechargeAction extends SuperAction {
	private static final long serialVersionUID = 2246644511126471393L;

	@Resource(name = "memcachedClient")
	private MemCachedClient memCachedClient;
	List<Permission> permissionList;
	private String serviceCode;

	// 后台左边菜单
	public String menu() {
		List<String> codeList = CommonUtil.gson().fromJson(
				this.getCurrentOperator().getServiceCode(),
				new TypeToken<List<String>>() {
				}.getType());
		if (codeList.size() > 0) {
			List<Permission> ob = this.getMenuLv2(serviceCode, codeList);
			if (ob instanceof ArrayList) {
				List<Permission> retList = (ArrayList<Permission>) ob;
				for (Permission p : retList) {
					if (permissionList == null)
						permissionList = new ArrayList<Permission>();
					if (p != null)
						permissionList.add(p);
				}
			}
		}
		return "menu";
	}

	public List<Permission> getMenuLv1(List<String> list) {
		List<Permission> list2 = new ArrayList<Permission>();
		Object ob = memCachedClient.get(Constants.MENU1);
		if (ob instanceof HashMap) {
			Map<String, Permission> map = (HashMap<String, Permission>) ob;
			if (list != null) {
				for (String co : list) {
					Permission p = map.get(co);
					if (p != null)
						list2.add(p);
				}
			}
		}
		return list2;
	}

	public List<Permission> getMenuLv2(String code, List<String> codeList) {
		List<Permission> list = new ArrayList<Permission>();
		Object ob = memCachedClient.get(Constants.MENU2);
		if (ob instanceof ArrayList) {
			ArrayList<Permission> list2 = (ArrayList<Permission>) ob;
			for (Permission p : list2) {
				if (p.getParentCode().equals(code)
						&& codeList.contains(p.getCode())) {
					p.setList(getMenuLv3(p.getCode(), codeList));
					list.add(p);
				}
			}
		}
		return list;
	}

	public List<Permission> getMenuLv3(String code, List<String> codeList) {
		List<Permission> list = new ArrayList<Permission>();
		Object ob = memCachedClient.get(Constants.MENU3);
		if (ob instanceof ArrayList) {
			ArrayList<Permission> list2 = (ArrayList<Permission>) ob;
			for (Permission p : list2) {
				if (p.getParentCode().equals(code)
						&& codeList.contains(p.getParentCode())) {
					list.add(p);
				}
			}
		}
		return list;
	}

	public List<Permission> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Permission> permissionList) {
		this.permissionList = permissionList;
	}

	public String getServiceCode() {
		return serviceCode;
	}

	public void setServiceCode(String serviceCode) {
		this.serviceCode = serviceCode;
	}
}
